//menu 对象
var Menu = {
	id:"menuTable",		//表格id
	seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    //初始化表格的列
    initColumn:function () {
        var columns = [
            {field: 'selectItem', radio: true},
            {title: '序号', field: 'id', visible: false, align: 'center', valign: 'middle',width:'50px'},
            {title: '菜单名称', field: 'menuName',  valign: 'middle', sortable: true,width:'17%'},
            {title: '菜单编号', field: 'menuCode', align: 'center', valign: 'middle', sortable: true,width:'12%'},
            //{title: '菜单父编号', field: 'pcode', align: 'center', valign: 'middle', sortable: true},
            {title: '请求地址', field: 'url', align: 'center', valign: 'middle', sortable: true,width:'15%'},
            {title: '排序', field: 'sortNum', align: 'center', valign: 'middle', sortable: true},
            {title: '层级', field: 'level', align: 'center', valign: 'middle', sortable: true},
            {title: '是否是菜单', field: 'isMenu', align: 'center', valign: 'middle', sortable: true,
                formatter:function(value){
                    if(value==1){
                        return "是";
                    }
                    return "不是";
                }
            },
            {title: '是否启用', field: 'isEnable', align: 'center', valign: 'middle', sortable: true,
                formatter:function(value){
                    if(value==1){
                        return "已启用";
                    }
                    return "<span style='color:red'>未启用</span>";
                }
            }]
        return columns;
    },
    //检查是否选中
    check:function(){
        var selected = $('#' + this.id).bootstrapTreeTable('getSelections');
        if (selected.length == 0) {
            Base.info("请先选中表格中的某一记录！");
            return false;
        } else {
            Menu.seItem = selected[0];
            return true;
        }
    },
    //添加窗口
    addWindow:function () {
        var index = layer.open({
            type: 2,
            title: '添加菜单',
            area: ['830px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Base.adminPath + '/sysMenu/add'
        });
        this.layerIndex = index;
    },
    editWindow:function () {
        if (this.check()) {
            var index = layer.open({
                type: 2,
                title: '修改菜单',
                area: ['800px', '450px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: Base.adminPath + '/sysMenu/edit/' + this.seItem.id
            });
            this.layerIndex = index;
        }
    },
    doDelete:function () {
        if (this.check()) {
            var operation = function(){
                var ajax = new $ajax(Base.adminPath + "/sysMenu/delete", function (data) {
                    Base.success("删除成功!");
                    Menu.table.refresh();
                }, function (data) {
                    Base.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("id", Menu.seItem.id);
                ajax.start();
            };

            Base.confirm("是否刪除该菜单?", operation);
        }
    },
    doSearch:function () {
        var queryData = {};

        queryData['menuName'] = $("#menuName").val();
        queryData['level'] = $("#level").val();

        Menu.table.refresh({query: queryData});
    },
    resetSearch:function(){
    	$("#menuName").val("");
        $("#level").val("");

        Menu.doSearch();
    }
}


$(function () {
	layui.use(['layer'], function(){
	  var layer = layui.layer;
	});
    var defaultColunms = Menu.initColumn();
    var table = new BSTreeTable(Menu.id, Base.adminPath + "/sysMenu/list", defaultColunms);
    table.setExpandColumn(2);
    table.setIdField("id");
    table.setCodeField("id");
    table.setParentCodeField("parentId");
    table.setExpandAll(false);
    table.setHeight($(window).height()-290);
    table.init();
    Menu.table = table;
});